package com.wlys.player.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.wlys.player.R;
import com.wlys.player.WlysApplication;
import com.wlys.player.controler.player.interf.IPlayerEngine;
import com.wlys.player.controler.player.interf.OnPlayerState;
import com.wlys.player.model.Track;

public class PlayTrackActivity extends Activity
{
    TextView mTrackName;
    Button mTrackPre;
    Button mTrackPlay;
    Button mTrackNext;
    SeekBar mTrackSeekBar;

    private Track mTrack;
    private boolean mIsTrackPlaying;
    private boolean mIsSeekBarTouching;
    private OnPlayerState mPlayerState = new OnPlayerState()
    {

        @Override
        public void start()
        {
            mIsTrackPlaying = true;
        }

        @Override
        public void complete()
        {
            mIsTrackPlaying = false;
        }

        @Override
        public void seekTo(int time)
        {
            if (!mIsSeekBarTouching)
            {
                mTrackSeekBar.setProgress(time);
            }
        }

        @Override
        public void onError(MediaPlayer mp, int what, int extra)
        {
            mIsTrackPlaying = false;
        }

        @Override
        public void maxSize(int time)
        {
            mTrackSeekBar.setMax(time);
        }

    };

    public static void launch(Track track, Context context)
    {
        Intent i = new Intent(context, PlayTrackActivity.class);
        i.putExtra("track", track);
        context.startActivity(i);
    }

    public static void launch(Context context)
    {
        Intent i = new Intent(context, PlayTrackActivity.class);
        context.startActivity(i);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_track);
        findViewById();
        setListener();
//        WlysApplication.getInstrance().setPlayerState(mPlayerState);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        mTrack = (Track) getIntent().getSerializableExtra("track");
        if (null != mTrack)
        {
            getPlayerEngine().play(mTrack);
        }
    }

    private void findViewById()
    {
        mTrackName = (TextView) findViewById(R.id.text_musicName);
        mTrackPre = (Button) findViewById(R.id.button_pre);
        mTrackPlay = (Button) findViewById(R.id.button_play);
        mTrackNext = (Button) findViewById(R.id.button_next);
        mTrackSeekBar = (SeekBar) findViewById(R.id.seekBar);
    }

    private void setListener()
    {
        mTrackPre.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                getPlayerEngine().pre(mTrack);
            }
        });
        mTrackPlay.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mIsTrackPlaying)
                {
                    getPlayerEngine().pause();
                    mTrackPlay.setBackgroundResource(R.drawable.play_select);
                    
                }
                else
                {
                    getPlayerEngine().contin();
                    mTrackPlay.setBackgroundResource(R.drawable.pause_select);
                }
                mIsTrackPlaying = !mIsTrackPlaying;
            }
        });
        mTrackNext.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                getPlayerEngine().next(mTrack);
            }
        });
        mTrackSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {
                getPlayerEngine().seekTo(seekBar.getProgress());
                mIsSeekBarTouching = false;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {
                mIsSeekBarTouching = true;
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser)
            {
            }
        });
    }

    private IPlayerEngine getPlayerEngine()
    {
        return WlysApplication.getInstrance().getPlayerEngineControl();
    }
}
